package com.jltx.xq.relaxnewspro.adapter;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jltx.xq.relaxnewspro.activity.DailyDetailsActivity;
import com.jltx.xq.relaxnewspro.R;
import com.jltx.xq.relaxnewspro.api.DailyApi;
import com.jltx.xq.relaxnewspro.database.collection.ICache;
import com.jltx.xq.relaxnewspro.utils.HttpUtil;
import com.jltx.xq.relaxnewspro.utils.Settings;
import com.jltx.xq.relaxnewspro.utils.StoryBean;

/**
 * Created by bo on 2017/4/18.
 */

public class DailyAdapter extends BaseListAdapter<StoryBean,DailyAdapter.ViewHolder> {
    public DailyAdapter(Context mContext, ICache<StoryBean> mCache) {
        super(mContext, mCache);
    }

    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_daily, parent, false);
        ViewHolder vh = new ViewHolder(itemView);
        return vh;
    }

    @Override
    public void onBindViewHolder(final ViewHolder holder,final int position) {
        final StoryBean storyBean = getItem(position);
        holder.title.setText(storyBean.getTitle());
//        holder.image.setImageResource(R.drawable.aa);
        if(Settings.noPicMode && HttpUtil.isWIFI == false){
            holder.image.setImageURI(null);
        }else {
            holder.image.setImageURI(Uri.parse(storyBean.getImages()[0]));
        }
        holder.parentView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, DailyDetailsActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString(mContext.getString(R.string.id_url), DailyApi.daily_details_url+storyBean.getId());
                bundle.putString(mContext.getString(R.string.id_title),storyBean.getTitle());
                bundle.putString(mContext.getString(R.string.id_body),storyBean.getBody());
                bundle.putString(mContext.getString(R.string.id_imageurl),storyBean.getLargepic());
                bundle.putString(mContext.getString(R.string.id_small_image),storyBean.getImages()[0]);
                bundle.putInt(mContext.getString(R.string.id_id),storyBean.getId());
                if(isCollection){
                    bundle.putBoolean(mContext.getString(R.string.id_collection),true);
                }else {
                    bundle.putBoolean(mContext.getString(R.string.id_collection),storyBean.isCollected() == 1 ? true : false);
                }
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        private View parentView;
        private TextView title;
        private ImageView image;
        public ViewHolder(View itemView) {
            super(itemView);
            parentView = itemView;
            title = (TextView) parentView.findViewById(R.id.title);
            image = (ImageView) parentView.findViewById(R.id.image);
        }
    }


}
